package com.gnerv.pabu.basic.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.pabu.basic.core.dto.EmailConfigDTO;
import com.gnerv.pabu.basic.core.dto.EmailConfigSaveDTO;
import com.gnerv.pabu.basic.core.dto.EmailConfigSearchDTO;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.service.IEmailConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 基础平台-邮件表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "邮件配置管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/email/config")
public class EmailConfigController {

    @Resource
    private IEmailConfigService emailConfigService;

    @ApiOperation(value = "保存 邮件配置")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveEmailConfig(@RequestBody EmailConfigSaveDTO emailConfigSaveDTO) {
        emailConfigService.saveEmailConfig(emailConfigSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "新建 邮件配置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createEmailConfig(@RequestBody EmailConfigSaveDTO emailConfigSaveDTO) {
        emailConfigService.createEmailConfig(emailConfigSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "删除 邮件配置")
    @RequestMapping(value = "/delete/{emailConfigId}", method = RequestMethod.GET)
    public Result deleteByEmailConfigId(@PathVariable(value = "emailConfigId") String emailConfigId) {
        emailConfigService.deleteByEmailConfigId(emailConfigId);
        return Result.response();
    }

    @ApiOperation(value = "修改 邮件配置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateEmailConfig(@RequestBody EmailConfigSaveDTO emailConfigSaveDTO) {
        emailConfigService.updateEmailConfig(emailConfigSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "修改 邮件配置状态")
    @RequestMapping(value = "/{emailConfigId}/status/{status}", method = RequestMethod.GET)
    public Result updateEmailConfigStatus(@PathVariable(value = "emailConfigId") String emailConfigId,
                                         @PathVariable(value = "status") Integer status) {
        emailConfigService.updateEmailConfigStatus(emailConfigId, status);
        return Result.response();
    }

    @ApiOperation(value = "查询邮件配置 根据邮件配置逻辑主键")
    @RequestMapping(value = "/select/{emailConfigId}", method = RequestMethod.GET)
    public Result selectEmailConfig(@PathVariable(value = "emailConfigId") String emailConfigId) {
        EmailConfigDTO emailConfig = emailConfigService.selectEmailConfig(emailConfigId);
        return Result.response().data("emailConfig", emailConfig);
    }

    @ApiOperation(value = "查询邮件配置列表 带分页 支持模糊查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listEmailConfig(EmailConfigSearchDTO emailConfig,
                                     @RequestParam(value = "current", defaultValue = "1") Long current,
                                     @RequestParam(value = "size", defaultValue = "300") Long size) {
        Page<EmailConfigDTO> emailConfigs = emailConfigService.listEmailConfig(emailConfig, current, size);
        return Result.response().data("emailConfigs", emailConfigs);
    }

}
